<?php

/**
 * Server_Api_Destination
 * 目的地服务
 * Author: netman
 * DateTime:  2023/9/11 16:37
 */
class Service_Api_Destination extends Contract_Api_Destination
{
    use Trait_Api_Common;
    use Trait_Api_Image;
    use Trait_Api_Destination;
    use Trait_Api_Content;
    const TYPE_ID = 12;

    public function __construct()
    {

    }

    function query($params): array
    {
        if($params['type_id']){
            $fields = array(
                'a.id',
                'a.kindname',
                'a.pinyin',
                'a.pid',
                'a.displayorder',
                'a.litpic'
            );

            $model_info = Model_Model::get_module_info($params['type_id']);
            $product_dest_table =  $model_info['pinyin'] . '_kindlist';

            $builder = DB::select_array($fields)
                ->from(array('destinations', 'a'))
                ->join(array($product_dest_table, 'b'),'LEFT')
                ->on('a.id', '=', DB::expr('sline_b.kindid'))
                ->where('a.isopen','=', 1);
            if ($params['type_id']) {
                $builder->and_where(DB::expr(" and FIND_IN_SET({$params['type_id']}, sline_a.opentypeids)"));
            } else {
                $builder->and_where('a.isnav', '=', 1);
            }
            if ($params['keyword']) {
                $builder->where_open();
                $builder->where('a.kindname', 'like', "%{$params['keyword']}%");
                $builder->or_where('a.pinyin', 'like', "%{$params['keyword']}%");
                $builder->where_close();
            }
            $builder->group_by('a.id')
                ->order_by('b.displayorder', 'asc');
            $result = $builder->execute()
                ->as_array();
        }else{
            $fields = array(
                'id',
                'kindname',
                'pinyin',
                'pid',
                'displayorder',
                'litpic'
            );
            $builder = DB::select_array($fields)->from('destinations')
                ->where('isopen','=', 1);
            if ($params['type_id']) {
                $builder->and_where(DB::expr(" and FIND_IN_SET({$params['type_id']}, opentypeids)"));
            } else {
                $builder->and_where('isnav', '=', 1);
            }
            if ($params['keyword']) {
                $builder->where_open();
                $builder->where('kindname', 'like', "%{$params['keyword']}%");
                $builder->or_where('pinyin', 'like', "%{$params['keyword']}%");
                $builder->where_close();
            }
            $builder->group_by('id')->order_by('displayorder', 'asc');
            $result = $builder->execute()->as_array();
        }


        $data = array();
		if(!$params['type_id']){
			$data[0] = $this->get_hot_dest($fields, $params);
		}

        foreach ($result as $k=>$item) {
            if ($item['pid'] == 0) {
                //第一级
                unset($result[$k]);
                $this->get_child($result, $item['id'], $item);
                $data[] = $item;
            }
        }

        return $data;
    }

    function query_group_spell($params): array
    {
        $fields = array(
            'id',
            'kindname',
            'pinyin',
            'pid',
            'displayorder',
            'litpic'
        );
        $builder = DB::select_array($fields)->from('destinations')
            ->where('isopen','=', 1);
        if ($params['type_id']) {
            $builder->and_where(DB::expr(" and FIND_IN_SET({$params['type_id']}, opentypeids)"));
        } else {
            $builder->and_where('isnav', '=', 1);
        }
        if ($params['keyword']) {
            $builder->where_open();
            $builder->where('kindname', 'like', "%{$params['keyword']}%");
            $builder->or_where('pinyin', 'like', "%{$params['keyword']}%");
            $builder->where_close();
        }
        $builder->group_by('id')->order_by('pinyin', 'asc');
        $result = $builder->execute()->as_array();
        $data = array();
        foreach ($result as $item) {
            $key = strtoupper($item['pinyin'][0]);
            if( $item['pinyin'] ) {
                if (isset($data[$key])) {
                    array_push($data[$key]['list'], $item);
                } else {
                    $data[$key] = array('title' => $key, 'list' => array());
                    array_push($data[$key]['list'], $item);
                }
            }
        }
        return array_values($data);
    }

    function hot($params): array
    {
        $limit = isset($params['limit']) ? $params['limit'] : 9;
        $w     = isset($params['image_w']) ? $params['image_w'] : 218;
        $h     = isset($params['image_h']) ? $params['image_h'] : 218;
        $type_id = isset($params['type_id']) ? $params['type_id'] : 0;
        //传了type_id使用关联表去查询是否热门
        if ($type_id && $type_id != self::TYPE_ID) {
            $model_info = Model_Model::get_module_info($params['type_id']);
            $product_dest_table =  $model_info['pinyin'] . '_kindlist';
            $builder = DB::select('destinations.id', 'destinations.kindname', 'destinations.pinyin', 'destinations.litpic', 'destinations.displayorder', 'destinations.jieshao')
                ->from('destinations')
                ->join($product_dest_table,'LEFT')
                ->on('destinations.id', '=',$product_dest_table.'.kindid')
                ->where($product_dest_table.'.ishot', '=', 1)
                ->and_where('isopen', '=', 1)
                ->and_where(DB::expr("find_in_set(".$type_id.",opentypeids)"), '>', 0)
                ->group_by('destinations.id')
                ->order_by('destinations.displayorder', 'asc');
        } else {
            $builder = DB::select('id', 'kindname', 'pinyin', 'litpic', 'displayorder', 'jieshao')
                ->from('destinations')
                ->where('ishot', '=', 1)
                ->and_where('isopen', '=', 1)
                ->limit($limit)
                ->group_by('id')
                ->order_by('displayorder', 'asc');

        }
        $list  = $builder->execute()->as_array();
        foreach ($list as &$v) {
            $v['litpic']  = $this->img_url($v['litpic'], $w, $h);
            $v['jieshao'] = St_Functions::cutstr_html($v['jieshao'], 1024);
        }
        return $list;
    }

    function index_columns($params): array
    {
        $type_id  = $params['type_id'];
        $model   = DB::select('maintable', 'id')->from('model')
            ->where('isopen', '=', 1)
            ->and_where('id', '=', $type_id)
            ->execute()->current();
        if (!$model) {
            throw new Exception("type_id对应模型不存在");
        }
        $product_table      = $model['maintable'];
        $product_dest_table = $model['maintable'] . "_kindlist";
        $data = DB::select_array(array('a.kindname','a.pinyin','a.id','a.displayorder'))
            ->from(array('destinations','a'))
            ->join(array($product_dest_table,'b'),'left')
            ->on('a.id','=', 'b.kindid')
            ->where('a.isopen', '=', 1)
            ->and_where('b.isnav', '=', 1)
            ->and_where(DB::expr(" AND FIND_IN_SET({$type_id},sline_a.opentypeids)"))
            ->group_by('a.id')
            ->order_by('b.displayorder','asc')
            ->order_by('a.displayorder', 'asc')
            ->execute()
            ->as_array();

        $arr = array();
        foreach ($data as $datum) {
            $num = DB::select()
                ->from($product_table)
                ->where('ishidden', '=',0)
                ->and_where(DB::expr(" AND FIND_IN_SET({$datum['id']}, kindlist)"))
                ->execute()
                ->count();
            if ($num) {
                $arr[] = $datum;
                if( count($arr) >= 10 ){
                    break;
                }
            }
        }
        return $arr;
    }

    /**
     * 目的地详情
     * @param $dest_id
     * @return array
     * User: netman
     * Date: 2023/9/20 14:57
     */
    function detail($dest_id): array
    {
        $data = array();
        $destination = DB::select('kindname', 'jieshao', 'litpic', 'piclist','pinyin')->from('destinations')->where('id', '=', $dest_id)->execute()->current();
        $destination['litpic'] = $this->img_url($destination['litpic']);
        $piclist = explode(',', $destination['piclist']);
        $pics = array();
        foreach ($piclist as &$item) {
           $pic = explode('||', $item);
           $pics[] = $this->img_url($pic[0]);
        }
        $destination['piclist'] = $pics;
        $jieshao = $destination['jieshao'];
        //简短介绍
        $destination['description'] = St_Functions::cutstr_html($jieshao,100);
        $destination['jieshao'] = $this->content_add_domain($jieshao);
        if(St_Functions::is_system_app_install(6)){
            $photo_num=St_Functions::get_dest_relevant_img($dest_id);
        }else{
            $photo_num=0;
        }
        $destination['photo_num'] = $photo_num;
        //主导航开启的栏目id
        $type_id_list = DB::select('m_typeid')
            ->from('m_nav')
            ->where('m_issystem', '=', 1)
            ->and_where('m_isopen', '=', 1)
            ->order_by('m_displayorder', 'asc')
            ->execute()
            ->as_array();
        //产品
        $product = $this->get_product_by_dest_id($type_id_list, $dest_id);
        //攻略,游记
        $article = $this->get_article_by_dest_id($type_id_list, $dest_id);
        $data['destination'] = $destination;
        $data['product'] = $product;
        $data['article'] = $article;
        $data['is_photo_open'] = St_Functions::checkModuleOpen(6);
        return $data;
    }


}